﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cotizav.CONEXION;
using System.Transactions;

namespace Cotizav
{
    public class CtrlFamilia
    {
        ConexionDataContext conexion;
        
        internal List<FAMILIA> getListFamilia()
        {
            using (conexion = new ConexionDataContext())
            {
                try
                {
                    return conexion.FAMILIA.Where(x => x.eliminado == 0).ToList();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        internal FAMILIA getFamilia(string idSelectedNode)
        {
            using (conexion = new ConexionDataContext())
            {
                try
                {
                    return conexion.FAMILIA.SingleOrDefault(x => x.idFamilia == idSelectedNode && x.eliminado == 0);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        internal bool actualizarFamilia(string idFamilia, FAMILIA fam)
        {
            using (conexion = new ConexionDataContext())
            {
                bool result = false;
                try
                {
                    FAMILIA f = conexion.FAMILIA.SingleOrDefault(x => x.idFamilia == idFamilia && x.eliminado == 0);
                    f.nombreFamilia = fam.nombreFamilia;
                    
                    conexion.SubmitChanges();
                    result = true;
                    return result;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        internal bool registrarFamilia(FAMILIA fam)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                using (conexion = new ConexionDataContext())
                {
                    bool result = false;
                    try
                    {
                        //insertamos el id
                        int correlativoFamilia = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").FAMILIA.Value;
                        string num = Convert.ToString(correlativoFamilia);
                        string numero = num.Trim().PadLeft(11, '0');
                        string idFamilia = "FAM_" + numero;

                        fam.idFamilia = idFamilia;

                        conexion.FAMILIA.InsertOnSubmit(fam);
                        conexion.SubmitChanges();
                        conexion.increm_ContFamilia();
                        transactionScope.Complete();
                        result = true;
                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                } 
            }
        }
    }
}
